﻿/**
 * 程序名称：任务执行框架JJYChengTaskWinForm-
 * 作者：jjycheng
 * 博客：https://blog.csdn.net/cplvfx
 * gitee:https://gitee.com/jjycheng
 **/
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JJYChengTaskWinForm.Factory.Common
{
    public class SqlSugarBase
    {
       public static SqlSugarClient db = null;
        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="ConnectionString">连接符字串</param>
        /// <param name="Dbtype">数据库类型</param>
        public static void SqlSugarBaseInit(string connectionString, DbType dbType)
        {
            db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString,//连接符字串
                DbType = dbType,//数据库类型
                LanguageType = LanguageType.Chinese,//只显示中文
                IsAutoCloseConnection = true //是否自动关闭连接，不设成true要手动close
            },
              db =>
              {
                  db.Aop.OnLogExecuting = (sql, pars) =>
                  {
                      //输出sql,查看执行sql 性能无影响
                      Console.WriteLine(sql);
                      //获取原生SQL推荐 5.1.4.63  性能OK
                      Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
                  };
              });
            db.Ado.CommandTimeOut = 30;//单位秒
            //IsValidConnection()返回true是数据库连接成功 
        }
    }
}
